<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015-3-18
 * Time: 13:43
 */

namespace doumiaoduo\buildform;


use yii\base\Component;
use yii\helpers\ArrayHelper;
use yii\base\ErrorException;

class BuildFormElement extends Component{

    public static $input_config=[
        'text'=>[
            'fun'=>'textInput',
        ],
        'textarea'=>[
            'fun'=>'textarea',
        ],
        'password'=>[
            'fun'=>'passwordInput',
        ],
        'dropdownlist'=>[
            'fun'=>'dropDownList',
        ],
        'radio'=>[
            'fun'=>'radio'
        ],
        'radiolist'=>[
            'fun'=>'radioList',
        ],
        'checkbox'=>[
            'fun'=>'checkbox',
        ],
        'checkboxlist'=>[
            'fun'=>'checkboxList'
        ],
        'widget'=>[
            'fun'=>'widget'
        ],
        'hidden'=>[
            'fun'=>'hiddenInput'
        ]
    ];

    public static function label($config,$model,$element){//���ã�ģ�����ƣ��ֶ�����
        $label=ArrayHelper::getValue($config,'models.'.$model.'.elements.'.$element.'.label');
        if(!empty($label)) return $label;
        $model=ArrayHelper::getValue($config,'models.'.$model.'.model');
        return $model->attributeLabels()[$element];
    }

    public static function field($form,$config,$model,$element){
        $hint=ArrayHelper::getValue($config,'models.'.$model.'.elements.'.$element.'.hint');
        $model_object=ArrayHelper::getValue($config,'models.'.$model.'.model');
        $field=!empty($hint)?$form->field($model_object,$element,['template'=>'{input}{hint}{error}'])->hint($hint):$form->field($model_object,$element,['template'=>'{input}{error}']);
        $element_config=ArrayHelper::getValue($config,'models.'.$model.'.elements.'.$element);
        return self::renderInput($field,$element_config);
    }

    private static function renderInput($field,$element_config){
        if(!isset(self::$input_config[$element_config['type']])){
            throw new ErrorException(\Yii::t('app','Sorry, the element type is not null'));
        }
        $type_config=self::$input_config[$element_config['type']];
        if($element_config['type']=='widget'&&!isset($element_config['class'])){
            throw new ErrorException(\Yii::t('app','Sorry, the element is widget need class'));
        }
        if($type_config['fun']=='widget'){
            return call_user_func_array([$field, $type_config['fun']], self::getWidgetParam($element_config));//����#field���е� $type_config['fun']����������self::getWidgetParam($element_config)Ϊ��������
        }
        return call_user_func_array([$field, $type_config['fun']], self::getParam($element_config));

    }

    private static function getParam($element_config){
        $param=[];
        $config=$element_config;
        unset($config['type']);
        if(isset($config['class'])){$param[]=$config['class'];unset($config['class']);}
        if(isset($config['items'])){$param[]=$config['items'];unset($config['items']);}
        if(isset($config['hint'])){unset($config['hint']);}
        if(isset($config['label'])){unset($config['label']);}
        if(isset($config['template'])){unset($config['template']);}
        $config=!empty($config)?$config:[];
        $param=ArrayHelper::merge($param,$config);
        return $param;
    }

    private static function getWidgetParam($element_config){
        $param=[];
        $config=$element_config;
        unset($config['type']);
        if(isset($config['class'])){$param[]=$config['class'];unset($config['class']);}
        if(isset($config['items'])){$param[]=$config['items'];unset($config['items']);}
        if(isset($config['hint'])){unset($config['hint']);}
        if(isset($config['label'])){unset($config['label']);}
        if(isset($config['template'])){unset($config['template']);}
        $param[]=$config;
//        var_dump($param);die;
        return $param;
    }
}